package main

import (
	middleware "CMETO/middlewares"
	"CMETO/routers"
	"CMETO/utils"
	"os/exec"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"

	docs "CMETO/docs"

	swaggerfiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

// @title RBAC权限管理系统
// @version 1.0.0
// @description RBAC权限管理的接口文档
// @host localhost:8002
// @BasePath /api/v1
// @schemes http https
// @securityDefinitions.apikey JWT
// @in header
// @name Authorization
func main() {
	// gin.SetMode(gin.DebugMode)
	r := gin.Default()
	r.Static("/static", "./static")
	logrus.AddHook(utils.FileWriter{})
	logrus.SetLevel(logrus.DebugLevel)
	logrus.SetFormatter(middleware.MyFomatter{Prefix: "CMETO"})
	r.Use(middleware.LogMiddleWare())
	r.Use(middleware.Cors())
	r.SetTrustedProxies([]string{"*"})

	docs.SwaggerInfo.BasePath = "/api/v1"
	v1 := r.Group("/api/v1")
	{
		routers.MenuApi(v1)
		routers.UserApi(v1)
		routers.RoleApi(v1)
		routers.DeptApi(v1)
		routers.FileApi(v1)
		routers.DictApi(v1)
		routers.CourseApi(v1)
		routers.GoodsApi(v1)
		routers.NoticeApi(v1)
		routers.OrderApi(v1)
		routers.AppApi(v1)
	}

	// 清理日志定时任务
	go utils.RemoveLog()
	// 生成swagger文档
	cmd := exec.Command("swag init")
	err := cmd.Run()
	if err != nil {
		print("生成接口文档成功\n")
	} else {
		print("生成接口文档失败\n")
	}
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
	r.Run("localhost:8002")
}
